<?php

class Admin_UserController extends Admin_Library_Controller_Action
{

    public function init()
    {
        parent::init();
        $this->_model = new Model_User();
        $this->_form = new Admin_Form_User();

        if (in_array($this->getRequest()->getActionName(), array('index', 'list'))) {
            $config = Zend_Registry::get('configuration');
            $page = 1;
            if (isset($this->_params['page']) && $this->_params['page'] > 0) {
                $page = $this->_params['page'];
            }
            $limit = $config->site->backend->paging->items_per_page;
            if (isset($this->_params['itemperpage']) && isset($this->_params['itemperpage']) > 0) {
                $limit = $this->_params['itemperpage'];
            }
            $orders = array('user.created_date DESC', 'user.firstname');
            $wheres = array();
            $orWheres = array();
            $likes = array();
            $orLikes = array();

            if (isset($this->_params['keyword']) && $this->_params['keyword'] !== '') {
                $likes['user.firstname'] = '%' . strip_tags($this->_params['keyword']) . '%';
                $likes['user.lastname'] = '%' . strip_tags($this->_params['keyword']) . '%';
                $likes['user.username'] = '%' . strip_tags($this->_params['keyword']) . '%';
                $this->view->assign('keyword', strip_tags($this->_params['keyword']));
            }
            if (isset($this->_params['published']) && $this->_params['published'] != '') {
                $wheres['user.published'] = intval($this->_params['published']);
                $this->view->assign('published', intval($this->_params['published']));
            }

            $this->_page = $page;
            $this->_limit = $limit;
            $this->_orders = $orders;
            $this->_wheres = $wheres;
            $this->_orWheres = $orWheres;
            $this->_likes = $likes;
            $this->_orLikes = $orLikes;
        }
        else if (in_array($this->getRequest()->getActionName(), array('add', 'edit'))) {
            $this->view->headLink()->appendStylesheet($this->view->themePath . '/css/datepicker.css');
            $this->view->headScript()->appendFile($this->view->themePath . '/js/bootstrap-datepicker.js', 'text/javascript');
            $this->view->headScript()->appendFile($this->view->themePath . '/js/admin-user.js', 'text/javascript');
        }
    }

    public function indexAction()
    {
        $this->_forward('list');
    }

    public function addAction()
    {
        if ($this->getRequest()->isPost() && $this->_form->isValid($this->getRequest()->getPost())) {
            // Processing for upload file
            if ($this->_form->file->receive()) {
                $image = $this->_form->file->getFileName('', false);
                $this->getRequest()->setParam('image', $image);
            }
            $result = $this->save();
            $this->_helper->flashMessenger->addMessage($result);
            if ($result['result'] == 'success') {
                $this->_helper->redirector->gotoRoute($this->_defaultUrlOptions);
            }
        }

        $this->view->form = $this->_form;
    }

    public function editAction()
    {
        if ($this->getRequest()->isPost() && $this->_form->isValid($this->getRequest()->getPost())) {
            // Processing for upload file
            if ($this->_form->file->receive()) {
                $image = $this->_form->file->getFileName('', false);
                $this->getRequest()->setParam('image', $image);
                //$oldFile = $this->getRequest()->getParam('oldFile');
                //@unlink($this->view->config->upload->path . DS . 'images' . DS . $oldFile);
            }
            $result = $this->save();
            $this->_helper->flashMessenger->addMessage($result);
            if ($result['result'] == 'success') {
                $this->_helper->redirector->gotoRoute($this->_defaultUrlOptions);
            }
        }

        $item = $this->_model->find(intval($this->getRequest()->getParam('id')));
        $values = $item->toArray();
        if ($values['published'] == 1) {
            $values['published'] = 'yes';
        } else if ($values['published'] == 0) {
            $values['published'] = 'no';
        }
        if(empty($values['image']) && $values['gender'] == 0) {
            $values['image'] = 'no_image_female.png';
        }
        if(empty($values['image']) && $values['gender'] == 1) {
            $values['image'] = 'no_image_male.png';
        }
        $values['dob'] = date('d-m-Y', strtotime($values['dob']));
        $this->_form->populate($values);

        $this->view->form = $this->_form;
        $this->_helper->viewRenderer('add');
    }

}